<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品编号" prop="goodsCode">
        <el-input
          v-model="queryParams.goodsCode"
          placeholder="请输入商品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入货品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:goods:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:goods:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:goods:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="goodsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
      <el-table-column
        label="商品编号"
        align="center"
        prop="goodsCode"
        min-width="150"
      />
      <el-table-column
        label="货品名称"
        align="center"
        prop="goodsName"
        min-width="150"
      />
      <el-table-column
        label="商品类别名称"
        align="center"
        prop="productCategoryName"
        min-width="100"
      />
      <el-table-column
        label="商品价格"
        align="center"
        prop="goodsPrice"
        min-width="100"
      />
      <!-- <el-table-column
        label="主图图片"
        align="center"
        prop="picPath"
        min-width="100"
      /> -->
      <el-table-column
        label="商品总数量"
        align="center"
        prop="productTotal"
        min-width="100"
      />
      <el-table-column
        label="商品销量"
        align="center"
        prop="productSales"
        min-width="100"
      />
      <el-table-column
        label="是否推荐"
        align="center"
        prop="recommendStatus"
        min-width="100"
      >
        <template slot-scope="scope">
          <el-switch
            :value="scope.row.recommendStatus"
            :active-value="0"
            :inactive-value="1"
            @change="handleRecSwitchChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column
        label="是否上架"
        align="center"
        prop="publishStatus"
        min-width="100"
      >
        <template slot-scope="scope">
          <el-switch
            :value="scope.row.publishStatus"
            :active-value="0"
            :inactive-value="1"
            @change="handleSwitchChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column
        label="创建人"
        align="center"
        prop="createUserId"
        min-width="100"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="lx-flex-row">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:goods:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:goods:remove']"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-tickets"
              @click="handleView(scope.row)"
              >详情</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <edit-goods
      v-if="isOpen"
      :visiable.sync="isOpen"
      :id="currentId"
      @confirm="handleQuery()"
    ></edit-goods>

    <el-dialog title="产品详情" :visible.sync="detailVisible" width="800px">
      <ProductDetail v-if="detailVisible" :id="productId"></ProductDetail>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGoods,
  delGoods,
  updateGoods,
  changePublicStatus,
  changeRecommendStatus,
} from "@/api/goodsManage/goods";
import EditGoods from "./cmp/EditGoods";
import ProductDetail from "@/components/ProductDetail";

export default {
  name: "Goods",
  components: {
    EditGoods,
    ProductDetail,
  },
  filters: {
    isTure(value) {
      return value ? true : false;
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      currentId: null, // 当前选择的商品
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      goodsList: [],
      // 是否显示弹出层
      isOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsCode: null,
        goodsName: null,
        productCategoryId: null,
        productCategoryName: null,
        goodsPrice: null,
        picPath: null,
        productSort: null,
        productTotal: null,
        productSales: null,
        detailHtml: null,
        recommendStatus: null,
        publishStatus: null,
        createUserId: null,
        updateUserId: null,
      },
      showPubSwitchIdx: -1,
      showRecSwitchIdx: -1,
      detailVisible: false,
      productId: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then((response) => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.currentId = null;
      this.isOpen = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.currentId = row.id;
      this.isOpen = true;
    },
    // 切换
    handleSwitchChange(row) {
      this.$confirm(
        "确认" +
          (row.publishStatus ? "上架" : "下架") +
          "商品信息编号为" +
          row.goodsCode +
          "的数据项？",
        "系统提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let data = {
            id: row.id,
            publishStatus: row.publishStatus ? 0 : 1,
          };
          // 调用接口
          changePublicStatus(data).then((res) => {
            if (res.code === 200) {
              this.getList();
              this.$modal.msgSuccess("修改成功");
              this.$emit("confirm");
            } else {
              this.$modal.msgSuccess(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    handleRecSwitchChange(row) {
      this.$confirm(
        "确认" +
          (row.recommendStatus ? "推荐" : "取消推荐") +
          "商品信息编号为" +
          row.goodsCode +
          "的数据项？",
        "系统提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let data = {
            id: row.id,
            recommendStatus: row.recommendStatus ? 0 : 1,
          };
          // 调用接口
          changeRecommendStatus(data).then((res) => {
            if (res.code === 200) {
              this.getList();
              this.$modal.msgSuccess("修改成功");
              this.$emit("confirm");
            } else {
              this.$modal.msgSuccess(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 查看详情
    handleView(row) {
      this.productId = row.id;
      this.detailVisible = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商品信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delGoods(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/goods/export",
        {
          ...this.queryParams,
        },
        `goods_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style>
@keyframes rotate {
  0% {
    -webkit-transform: rotate(0deg);
  }
  25% {
    -webkit-transform: rotate(90deg);
  }
  50% {
    -webkit-transform: rotate(180deg);
  }
  75% {
    -webkit-transform: rotate(270deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}

.loading-switch .el-switch__core::after {
  background-color: transparent;
  background-image: url("../../../assets/images/loading.png");
  background-size: 16px 16px;
  animation: rotate 1s linear infinite;
}
</style>
